<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <link rel="alternate" href="/atom.xml" title="刘攀的个人专栏" type="application/atom+xml">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    hostname: new URL('http://liupan.run').hostname,
    root: '/',
    scheme: 'Pisces',
    version: '7.5.0',
    exturl: false,
    sidebar: {"position":"right","Pisces | Gemini":240,"display":"post","offset":12,"onmobile":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: 'search.xml',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    sidebarPadding: 40
  };
</script>

  <meta name="description" content="JS随机中文字，Vue与Flask字界符修改  生成随机中文字符串123456789function randomChars(count) &amp;#123;    var str = &quot;&quot;;    for (var i = 0; i &amp;lt; count; i++) &amp;#123;        var temp = &amp;apos;0x&amp;apos; + (Math.round(Math.random() * 20901)">
<meta name="keywords" content="flask,js,vue,定界符">
<meta property="og:type" content="article">
<meta property="og:title" content="014-JS随机中文字符及Vue与Flask字界符修改">
<meta property="og:url" content="http:&#x2F;&#x2F;liupan.run&#x2F;2020&#x2F;01&#x2F;01&#x2F;014-JS%E9%9A%8F%E6%9C%BA%E4%B8%AD%E6%96%87%E5%AD%97%E7%AC%A6%E5%8F%8AVue%E4%B8%8EFlask%E5%AD%97%E7%95%8C%E7%AC%A6%E4%BF%AE%E6%94%B9&#x2F;index.html">
<meta property="og:site_name" content="刘攀的个人专栏">
<meta property="og:description" content="JS随机中文字，Vue与Flask字界符修改  生成随机中文字符串123456789function randomChars(count) &amp;#123;    var str = &quot;&quot;;    for (var i = 0; i &amp;lt; count; i++) &amp;#123;        var temp = &amp;apos;0x&amp;apos; + (Math.round(Math.random() * 20901)">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2020-01-01T15:03:36.398Z">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://liupan.run/2020/01/01/014-JS%E9%9A%8F%E6%9C%BA%E4%B8%AD%E6%96%87%E5%AD%97%E7%AC%A6%E5%8F%8AVue%E4%B8%8EFlask%E5%AD%97%E7%95%8C%E7%AC%A6%E4%BF%AE%E6%94%B9/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>014-JS随机中文字符及Vue与Flask字界符修改 | 刘攀的个人专栏</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">刘攀的个人专栏</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <p class="site-subtitle">面朝大海,春暖花开</p>
  </div>

  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签<span class="badge">28</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类<span class="badge">0</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档<span class="badge">20</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="none"
           placeholder="搜索..." spellcheck="false"
           type="text" id="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result"></div>

</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://liupan.run/2020/01/01/014-JS%E9%9A%8F%E6%9C%BA%E4%B8%AD%E6%96%87%E5%AD%97%E7%AC%A6%E5%8F%8AVue%E4%B8%8EFlask%E5%AD%97%E7%95%8C%E7%AC%A6%E4%BF%AE%E6%94%B9/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="liupan">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="刘攀的个人专栏">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          014-JS随机中文字符及Vue与Flask字界符修改
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-01-01 22:42:29 / 修改时间：23:03:36" itemprop="dateCreated datePublished" datetime="2020-01-01T22:42:29+08:00">2020-01-01</time>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <blockquote>
<p>JS随机中文字，Vue与Flask字界符修改</p>
</blockquote>
<h2 id="生成随机中文字符串"><a href="#生成随机中文字符串" class="headerlink" title="生成随机中文字符串"></a>生成随机中文字符串</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">randomChars</span>(<span class="params">count</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> str = <span class="string">""</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; count; i++) &#123;</span><br><span class="line">        <span class="keyword">var</span> temp = <span class="string">'0x'</span> + (<span class="built_in">Math</span>.round(<span class="built_in">Math</span>.random() * <span class="number">20901</span>) + <span class="number">19968</span>).toString(<span class="number">16</span>); </span><br><span class="line">        <span class="comment">//生成单个随机中文字符编号,每个字符编码样式为：'0x53c2'</span></span><br><span class="line">        str += <span class="built_in">String</span>.fromCharCode(temp);  <span class="comment">//从编码转换为字符</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> str;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<a id="more"></a>

<h2 id="Flask中的与Vue中的冲突解决方法"><a href="#Flask中的与Vue中的冲突解决方法" class="headerlink" title="Flask中的与Vue中的冲突解决方法"></a>Flask中的与Vue中的冲突解决方法</h2><p>Jinja2 有三种定界符语法：</p>
<ul>
<li>{ { x } } 用来标记变量；</li>
<li>{ % x % } 用来标记语句；</li>
<li>{ # x # } 用来标记注释；</li>
</ul>
<h3 id="1-使用-Jinja2-的-raw-标签标记-JavaScript-模板代码"><a href="#1-使用-Jinja2-的-raw-标签标记-JavaScript-模板代码" class="headerlink" title="1. 使用 Jinja2 的 raw 标签标记 JavaScript 模板代码"></a>1. 使用 Jinja2 的 raw 标签标记 JavaScript 模板代码</h3><p>第一种方式最直观，使用 Jinja2 的 raw 标签声明原生代码块，也就是不需要进行后端渲染的代码块。使用 <code>raw</code> 和 <code>endraw</code> 标签把 JavaScript 模板部分标记出来即可，比如：</p>
<figure class="highlight django"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="template-tag">&#123;% <span class="name">raw</span> %&#125;</span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>&gt;</span></span></span><br><span class="line"><span class="xml">    </span><span class="template-variable">&#123;&#123; js_var &#125;&#125;</span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="template-tag">&#123;% <span class="name">endraw</span> %&#125;</span></span><br></pre></td></tr></table></figure>

<p>这种方式的副作用最少，尽管需要多几行代码，但不会影响你写 Jinja2 或其他 JavaScript 库的语法习惯。</p>
<h3 id="2-修改-Jinja2-的语法定界符号"><a href="#2-修改-Jinja2-的语法定界符号" class="headerlink" title="2. 修改 Jinja2 的语法定界符号"></a>2. 修改 Jinja2 的语法定界符号</h3><p>第二种方式是修改 Jinja2 的语法定界符号，一般只修改变量定界符即可，其他的按需修改。具体通过修改程序实例的下面几个属性来实现：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">from flask import Flask</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line"></span><br><span class="line">app.jinja_env.block_start_string = &apos;(%&apos;  # 修改块开始符号</span><br><span class="line">app.jinja_env.block_end_string = &apos;%)&apos;  # 修改块结束符号</span><br><span class="line">app.jinja_env.variable_start_string = &apos;((&apos;  # 修改变量开始符号</span><br><span class="line">app.jinja_env.variable_end_string = &apos;))&apos;  # 修改变量结束符号</span><br><span class="line">app.jinja_env.comment_start_string = &apos;(#&apos;  # 修改注释开始符号</span><br><span class="line">app.jinja_env.comment_end_string = &apos;#)&apos;  # 修改注释结束符号</span><br></pre></td></tr></table></figure>

<h3 id="3-修改-JavaScript-模板的语法定界符号"><a href="#3-修改-JavaScript-模板的语法定界符号" class="headerlink" title="3. 修改 JavaScript 模板的语法定界符号"></a>3. 修改 JavaScript 模板的语法定界符号</h3><p>第三种方式是修改 JavaScript 模板的语法定界符号，具体方法因 JavaScript 模板/框架而异，可以参见相关文档了解。以 Vuejs 为例，下面将模板定界符改为中括号：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> app = <span class="keyword">new</span> Vue(&#123;</span><br><span class="line">  el: <span class="string">"#app"</span>,</span><br><span class="line">  delimiters: [<span class="string">"[["</span>, <span class="string">"]]"</span>],</span><br><span class="line">  data: &#123;</span><br><span class="line">    message: <span class="string">"Hello Vue!"</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="折中方案"><a href="#折中方案" class="headerlink" title="折中方案"></a>折中方案</h3><p>如果你觉得使用 <code>raw</code> 标签太麻烦，而修改语法定界符又不习惯，这里还有一个折中方法：两边都使用双花括号作为定界符，但根据花括号内部是否添加空格来进行区分。</p>
<p>具体来说，对 Jinja2 变量使用 Jinja2 标准语法，也就是使用 。实际示例如下：</p>
<figure class="highlight django"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="template-variable">&#123;&#123; jinja_var &#125;&#125;</span></span><br></pre></td></tr></table></figure>

<p>而 JavaScript 模板使用没有空格的双花括号，即：</p>
<figure class="highlight django"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="template-variable">&#123;&#123;js_var&#125;&#125;</span></span><br></pre></td></tr></table></figure>

<p>这是一种更适合心细的懒人的方法，如果是团队项目，可能会对不习惯这种用法的人造成困扰，记得在文档里注明。这种方式只需要修改 Jinja2 定界符：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">app.jinja_env.variable_start_string = &apos;&#123;&#123; &apos;</span><br><span class="line">app.jinja_env.variable_end_string = &apos; &#125;&#125;&apos;</span><br></pre></td></tr></table></figure>

<p>另外需要注意的是，如果你使用了其他 Flask 扩展的内置 Jinja2 模板或宏，需要确保它们都使用了包含空格的标准 Jinja2 语法。举例来说，用来方便集成 Bootstrap 的 Flask-Bootstrap 就没法使用，需要使用替代的 Bootstrap-Flask。</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/flask/" rel="tag"># flask</a>
              <a href="/tags/js/" rel="tag"># js</a>
              <a href="/tags/vue/" rel="tag"># vue</a>
              <a href="/tags/%E5%AE%9A%E7%95%8C%E7%AC%A6/" rel="tag"># 定界符</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2019/12/30/010-%E8%AF%97%E8%AF%8Djson%E5%AF%BC%E5%85%A5%E8%87%B3mariadb/" rel="prev" title="010-诗词json导入至mariadb">
      <i class="fa fa-chevron-left"></i> 010-诗词json导入至mariadb
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/01/02/016-Flask%E4%B8%AD.env%E5%8A%A0%E8%BD%BD%E5%8F%98%E9%87%8F%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9/" rel="next" title="016-Flask中.env加载变量注意事项">
      016-Flask中.env加载变量注意事项 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#生成随机中文字符串"><span class="nav-text">生成随机中文字符串</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Flask中的与Vue中的冲突解决方法"><span class="nav-text">Flask中的与Vue中的冲突解决方法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-使用-Jinja2-的-raw-标签标记-JavaScript-模板代码"><span class="nav-text">1. 使用 Jinja2 的 raw 标签标记 JavaScript 模板代码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-修改-Jinja2-的语法定界符号"><span class="nav-text">2. 修改 Jinja2 的语法定界符号</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-修改-JavaScript-模板的语法定界符号"><span class="nav-text">3. 修改 JavaScript 模板的语法定界符号</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#折中方案"><span class="nav-text">折中方案</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="liupan"
      src="/images/avatar.gif">
  <p class="site-author-name" itemprop="name">liupan</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">20</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">28</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="feed-link motion-element">
    <a href="/atom.xml" rel="alternate">
      <i class="fa fa-rss"></i>RSS
    </a>
  </div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/facetothesea" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;facetothesea" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="/mailto:hbxgliupan@sina.com" title="E-Mail → mailto:hbxgliupan@sina.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">liupan</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v4.0.0
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> v7.5.0
  </div>

        








        
      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script>
<script src="/js/schemes/pisces.js"></script>
<script src="/js/next-boot.js"></script>



  




  <script src="/js/local-search.js"></script>













  

  

  

</body>
</html>
